﻿using System.Net;
using System.Net.Http.Headers;
using Microsoft.AspNetCore.Authorization;
using Simple.Services.System.Application;
using Simple.Services.System.FileInfo;
using Simple.Services.System.FileInfo.Models;
using System.IO;
using System.Web;
using Simple.Common.Filters;

namespace Simple.WebApi.Controllers.System
{
    /// <summary>
    /// 应用管理
    /// </summary>
    [Route("api/sysfileinfo/[action]")]
    [ApiController]
    public class FileInfoController : ControllerBase
    {
        private readonly FileInfoService _fileInfoServeice;

        public FileInfoController(FileInfoService fileInfoServeice)
        {
            _fileInfoServeice = fileInfoServeice;
        }

        /// <summary>
        /// 文件查询
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public async Task<AppResult> Page([FromQuery] FilePageInputModel model)
        {
            PageResultModel<FileInfoModel> data = await _fileInfoServeice.GetPageAsync(model);
            return AppResult.Status200OK(data: data);
        }

        /// <summary>
        /// 文件上传
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public async Task<AppResult> Upload ([FromForm] FileInfoInputModel model)
        {
            var fileId = await _fileInfoServeice.UploadAsync(model);
            return AppResult.Status200OK("上传成功", fileId);
        }

        /// <summary>
        /// 文件批量上传
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [DisabledRequestRecord]
        public async Task<AppResult> UploadFiles([FromForm] FileInfoInputModel model)
        {
            var fileId = await _fileInfoServeice.UploadFilesAsync(model);
            return AppResult.Status200OK("上传成功", fileId);
        }

        /// <summary>
        /// 文件删除
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public async Task<AppResult> Delete([FromBody] IdInputModel model)
        {
            await _fileInfoServeice.DeleteAsync(model);
            return AppResult.Status200OK("删除成功");
        }

        ///// <summary>
        ///// 文件下载
        ///// </summary>
        ///// <param name="model"></param>
        ///// <returns></returns>
        //[HttpPost]
        //[Authorize]
        //public async Task<FileContentResult> Download([FromBody] IdInputModel model)
        //{
        //    var downInfo = await _fileInfoServeice.DownFile(model.Id);
        //    return File(downInfo.FileData, "application/octet-stream", fileDownloadName: HttpUtility.UrlEncode(downInfo.FileName));

        //}

        /// <summary>
        /// 文件下载
        /// </summary>
        /// 仅提供给MicrosoftOffice预览文件，因为是外部调用，不需要授权
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<FileContentResult> OfficePreView([FromQuery] Guid id)
        {
            var downInfo = await _fileInfoServeice.DownFile(id);
            return File(downInfo.FileData, "application/octet-stream", fileDownloadName: HttpUtility.UrlEncode(downInfo.FileName));

        }

        /// <summary>
        /// 文件预览
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public async Task<FileContentResult> Preview([FromQuery] IdInputModel model)
        {
            var downInfo = await _fileInfoServeice.DownFile(model.Id);
            return File(downInfo.FileData, "application/octet-stream", fileDownloadName: HttpUtility.UrlEncode(downInfo.FileName));

        }

        /// <summary>
        /// 文件预览
        /// </summary>
        /// 提供给图片预览的地址,无授权
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<FileContentResult> PreviewImg([FromQuery] IdInputModel model)
        {
            var downInfo = await _fileInfoServeice.DownFile(model.Id);
            return File(downInfo.FileData, "application/octet-stream", fileDownloadName: HttpUtility.UrlEncode(downInfo.FileName));

        }
        /// <summary>
        /// 新增文件夹
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        public async Task<AppResult> AddFolder([FromBody] FolderInputModel model)
        {
            await _fileInfoServeice.AddFolder(model);
            return AppResult.Status200OK("新增成功");

        }

        /// <summary>
        /// 获取续传信息
        /// </summary>
        /// <param name="folderId"></param>
        /// <param name="path"></param>
        /// <param name="fileName"></param>
        /// <param name="fileSize"></param>
        /// <param name="fileType"></param>
        /// <param name="blobCount"></param>
        /// <param name="blobSize"></param>
        /// <returns></returns>
        [HttpGet]
        [Authorize]
        public async Task<AppResult> GetFileResumableInfo(string? folderId, string? path, string? fileName, long? fileSize, string? fileType, long? blobCount, int? blobSize)
        {
            var result = await _fileInfoServeice.GetFileResumableInfo(folderId, path ?? "system", fileName, fileSize, fileType, blobCount, blobSize);
            return AppResult.Status200OK(data: result);
        }

        /// <summary>
        /// 切片上传
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [DisabledRequestRecordAttribute]
        public async Task<AppResult> UploadFile([FromForm] FileInfoSliceUpdloadInput input)
        {
            var result = await _fileInfoServeice.UploadFileBySlice(input);
            return AppResult.Status200OK("上传成功", data: result);
        }

        /// <summary>
        /// 取消上传
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        [HttpPost]
        [Authorize]
        [DisabledRequestRecordAttribute]
        public async Task<AppResult> CancelUpload(string key)
        {
            var result = await _fileInfoServeice.CancelUpload(key);
            return AppResult.Status200OK("取消成功", data: result);
        }

        /// <summary>
        /// 文件上传（适用于uniapp）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
/*        [Authorize]*/
        [DisabledRequestRecord]
        public async Task<AppResult> UploadFileInfo([FromForm] FileInfoInputModel1 model)
        {
            var fileId = await _fileInfoServeice.UploadFileAsync(model);
            return AppResult.Status200OK("上传成功", fileId);
        }
    }
}
